datacops-cms
Version:
A modern, extensible CMS built with Next.js and Prisma.
74 lines (63 loc) • 2.52 kB
text/typescript
/* eslint-disable @typescript-eslint/no-explicit-any */
import fs from "fs";
import { NextRequest, NextResponse } from "next/server";
import path from "path";
// Folder where content type schema files are stored
const contentTypesFolder = path.resolve(process.cwd(), "content-types");
// Path to API permissions file
const permissionsPath = path.resolve(process.cwd(), "content/api-permissions.json");
export async function GET(
req: NextRequest,
{ params }: { params: { type: string } }
)
{
const { type } = await params;
try {
const typeName = type.trim().toLowerCase(); // e.g. "blog"
const filePath = path.join(contentTypesFolder, `${typeName}.json`);
if (!fs.existsSync(filePath)) {
return NextResponse.json({ error: `Type "${typeName}" not found` }, { status: 404 });
}
const schema = JSON.parse(fs.readFileSync(filePath, "utf-8"));
return NextResponse.json(schema);
} catch (e: any) {
return NextResponse.json(
{ error: `Error reading schema for "${params.type}": ${e.message}` },
{ status: 500 }
);
}
}
export async function DELETE(
req: NextRequest,
{ params }: { params: { type: string } }
)
{
const { type } = await params;
if (!type) {
return NextResponse.json({ error: "Content type name is required." }, { status: 400 });
}
const filePath = path.join(contentTypesFolder, `${type}.json`);
if (!fs.existsSync(filePath)) {
return NextResponse.json({ error: "Content type not found." }, { status: 404 });
}
try {
// Delete the content type schema file
fs.unlinkSync(filePath);
// Remove from API permissions
let permissions: Record<string, any> = {};
if (fs.existsSync(permissionsPath)) {
permissions = JSON.parse(fs.readFileSync(permissionsPath, "utf-8"));
if (permissions[type]) {
delete permissions[type];
fs.writeFileSync(permissionsPath, JSON.stringify(permissions, null, 2));
}
}
// (Optional) TODO: Remove related data, trigger prisma regeneration, etc.
return NextResponse.json({ success: true, message: "Content type deleted." });
} catch (err: any) {
return NextResponse.json(
{ error: err.message || "Failed to delete content type." },
{ status: 500 }
);
}
}